/* 
 * oms，一个基于J2EE架构运营支撑平台
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.publish.impl;

import java.util.List;

import org.grayrabbit.cms.dao.publish.PublishConfigDao;
import org.grayrabbit.cms.entity.PublishConfig;
import org.grayrabbit.cms.vo.publish.PublishConfigBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>PublishConfig</b> dao implementation 

 * @version 1.0，创建时间：2012-01-05 17:51:19 
 */
@Repository
public class PublishConfigDaoImpl extends GenericHibernateDao<PublishConfig, String>
		implements PublishConfigDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From PublishConfig as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see PublishConfigDao#assembly(PublishConfigBean)
	 */
	public QueryCondition assembly(PublishConfigBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see PublishConfigDao#assemblyCount(PublishConfigBean)
	 */
	public QueryCondition assemblyCount(PublishConfigBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * @see PublishConfigDao#count(PublishConfigBean)
	 */
	public int count(PublishConfigBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see PublishConfigDao#assemblyQuery(PublishConfigBean)
	 */
	public QueryCondition assermblyQuery(PublishConfigBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see PublishConfigDao#assemblyQueryCount(PublishConfigBean)
	 */
	public QueryCondition assemblyQueryCount(PublishConfigBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see PublishConfigDao#findByPage(Page, PublishConfigBean)
	 */
	public List<PublishConfig> findByPage(Page<PublishConfig> page,
			PublishConfigBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void trans(PublishConfigBean bean,QueryCondition qc){
		//TODO:兄弟们此处根据业务修改代码，完成后，请删除本行，沈永生于2012-01-05 17:51:19提醒!
		if(bean!=null){
		            if(bean.getIsMore() != null){
				qc.add(" And obj.isMore like ? ", "%"+bean.getIsMore()+"%");
			}
            if(bean.getPublishObject() != null){
				qc.add(" And obj.publishObject like ? ", "%"+bean.getPublishObject()+"%");
			}
            if(bean.getObjectId() != null){
				qc.add(" And obj.objectId like ? ", "%"+bean.getObjectId()+"%");
			}
            if(bean.getPublishTime() != null){
				qc.add(" And obj.publishTime like ? ", "%"+bean.getPublishTime()+"%");
			}
            if(bean.getPublishInterval() != null){
				qc.add(" And obj.publishInterval like ? ", "%"+bean.getPublishInterval()+"%");
			}
            if(bean.getIsRun() != null){
				qc.add(" And obj.isRun like ? ", "%"+bean.getIsRun()+"%");
			}
            if(bean.getIsEnabled() != null){
				qc.add(" And obj.isEnabled like ? ", "%"+bean.getIsEnabled()+"%");
			}
            if(bean.getCreateLoginName() != null){
				qc.add(" And obj.createLoginName like ? ", "%"+bean.getCreateLoginName()+"%");
			}
            if(bean.getCreateRealName() != null){
				qc.add(" And obj.createRealName like ? ", "%"+bean.getCreateRealName()+"%");
			}
            if(bean.getCreateTime() != null){
				qc.add(" And obj.createTime like ? ", "%"+bean.getCreateTime()+"%");
			}
            if(bean.getUpdateLoginName() != null){
				qc.add(" And obj.updateLoginName like ? ", "%"+bean.getUpdateLoginName()+"%");
			}
            if(bean.getUpdateRealName() != null){
				qc.add(" And obj.updateRealName like ? ", "%"+bean.getUpdateRealName()+"%");
			}
            if(bean.getUpdateTime() != null){
				qc.add(" And obj.updateTime like ? ", "%"+bean.getUpdateTime()+"%");
			}

		}
	}


}